package com.example.grape_effect.gl.common

import android.opengl.ETC1Util
import android.opengl.GLES20
import com.example.grape_effect.base.GrapeEffect
import com.example.grape_effect.gl.glHelper.TextureHelper
import com.example.grape_effect.gl.glutils.ZipPkmReader

class PkmTexture(private val filePath:String): Texture() {
    var reader: ZipPkmReader = ZipPkmReader(GrapeEffect.grapeContext, filePath)
    override fun freeSource() {
        TODO("Not yet implemented")
    }
    var isOpen = false
    private lateinit var stickerTexture: IntArray
     fun prepareToDraw(imageLocation:Int, alphaLocation:Int) {
       if(!isOpen){
            reader.open()
            stickerTexture = IntArray(2)
            TextureHelper.createTexture(stickerTexture!!)
            width = 0
            height = 0
           isOpen = true
        }
            val t = reader.nextTexture
            val tAlpha = reader.nextTexture
            if (t != null && tAlpha != null) {
                width = t.width
                height = t.height
                GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, stickerTexture[0])
                ETC1Util.loadTexture(
                    GLES20.GL_TEXTURE_2D,
                    0,
                    0,
                    GLES20.GL_RGB,
                    GLES20.GL_UNSIGNED_SHORT_5_6_5,
                    t
                )
                GLES20.glUniform1i(imageLocation, 0)
                GLES20.glActiveTexture(GLES20.GL_TEXTURE1)
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, stickerTexture[1])
                ETC1Util.loadTexture(
                    GLES20.GL_TEXTURE_2D,
                    0,
                    0,
                    GLES20.GL_RGB,
                    GLES20.GL_UNSIGNED_SHORT_5_6_5,
                    tAlpha
                )
                GLES20.glUniform1i(alphaLocation, 1)
            } else {
                reader.close()
                reader.open()
            }
        }

    }




